package GUI;

import java.awt.Color;

import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Font;

import logic.Message;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.util.ArrayList;
import java.util.Calendar;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.UIManager;

public class Reports extends JPanel{
	
	private Client_GUI parent;
	private String toString;
	private JLabel lblwerning;
	private JTextField textFieldStartDate;
	private JTextField textFieldEndDate;
	private JLabel LBLEndDate;
	private JLabel LBLStartDate;
	private int action;
	
	
	public Reports(final Client_GUI parent, int action) {
		this.setBackground(UIManager.getColor("inactiveCaption"));
		this.action =action;
		this.parent = parent;
		this.setSize(Client_GUI.MAIN_WIDTH, Client_GUI.MAIN_HEIGHT);
		setLayout(null);
		
		switch (action)
		{
        case (Message.LATE_RETURN_REPORT_REQUEST):{
        	toString = "Late Return Report";
        	break;
        }
                 
        case Message.LOAN_REPORT_REQUEST:{
        	toString = "Loan Report";
        	break;
        }
    
    }
		
		JLabel label = new JLabel(toString);
		label.setForeground(Color.RED);
		label.setFont(new Font("Tahoma", Font.BOLD, 23));
		label.setBounds(253, 58, 289, 44);
		add(label);
		
		
		lblwerning = new JLabel("there are missing fields!!!!");
		lblwerning.setForeground(Color.RED);
		lblwerning.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblwerning.setBounds(331, 353, 193, 24);
		lblwerning.setVisible(false);
		add(lblwerning);
		
		JLabel lblStartDate = new JLabel("Start Date:");
		lblStartDate.setFont(new Font("Tahoma", Font.BOLD, 15));
		lblStartDate.setBounds(191, 196, 102, 30);
		add(lblStartDate);
		
		textFieldStartDate = new JTextField();
		textFieldStartDate.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent arg0) {
				parent.safeToClear = false;
				lblwerning.setVisible(false);
			}
		});
		textFieldStartDate.addFocusListener(new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent arg0) {
				ValidateData(false);
			}
		});
		textFieldStartDate.setBounds(321, 196, 248, 30);
		add(textFieldStartDate);
		textFieldStartDate.setColumns(10);
		
		LBLStartDate = new JLabel("");
		LBLStartDate.setBounds(585, 196, 30, 30);
		add(LBLStartDate);
		
		JLabel lblEndDate = new JLabel("End Date:");
		lblEndDate.setFont(new Font("Tahoma", Font.BOLD, 15));
		lblEndDate.setBounds(191, 277, 102, 30);
		add(lblEndDate);
		
		textFieldEndDate = new JTextField();
		textFieldEndDate.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent arg0) {
				parent.safeToClear = false;
				lblwerning.setVisible(false);
				
			}
		});
		textFieldEndDate.addFocusListener(new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent arg0) {
				ValidateData(false);
			}
		});
		textFieldEndDate.setColumns(10);
		textFieldEndDate.setBounds(321, 277, 248, 30);
		add(textFieldEndDate);
		
		LBLEndDate = new JLabel("");
		LBLEndDate.setBounds(585, 277, 30, 30);
		add(LBLEndDate);
		
		JButton btnNewButton = new JButton("Get Report");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				ValidateData(true);
			}
		});
		btnNewButton.setFont(new Font("Tahoma", Font.BOLD, 13));
		btnNewButton.setBounds(101, 421, 183, 44);
		add(btnNewButton);
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				Home home = new Home(parent,parent.client.publicMessages);
				VerifyDialog verify = new VerifyDialog(parent, home,false);

			}
		});
		btnCancel.setFont(new Font("Tahoma", Font.BOLD, 13));
		btnCancel.setBounds(532, 421, 193, 44);
		add(btnCancel);
		
		JLabel lblExampelYyyymmdd = new JLabel("exampel :[ YYYY-MM-DD ]");
		lblExampelYyyymmdd.setFont(new Font("Tahoma", Font.BOLD, 11));
		lblExampelYyyymmdd.setBounds(354, 247, 183, 14);
		add(lblExampelYyyymmdd);
	
		
	}
	
	
	private void ValidateData(boolean fromOK){
		
		ImageIcon okImageIcon = new ImageIcon(getClass().getClassLoader().getResource("resource/ok.png"));
		ImageIcon OK = new ImageIcon (okImageIcon.getImage().getScaledInstance(24, 24, java.awt.Image.SCALE_SMOOTH));
		
		ImageIcon badImageIcon = new ImageIcon(getClass().getClassLoader().getResource("resource/bad.png"));
		ImageIcon BAD = new ImageIcon (badImageIcon.getImage().getScaledInstance(24, 24, java.awt.Image.SCALE_SMOOTH));
		
		boolean valid = true;
		
		String temp; // the string to be tested on each test
		
	
		//validate start date
		temp = textFieldStartDate.getText();
		if(!temp.equals(""))
		try
		{
			if ((temp.charAt(4) == '-' && temp.charAt(7) == '-') || (temp.charAt(4) == '/' && temp.charAt(7) == '/') || (temp.charAt(4) == '.' && temp.charAt(7) == '.')) 
				LBLStartDate.setIcon(OK);
			else {
				LBLStartDate.setIcon(BAD);
				valid = false;
			}
			
			int year = Integer.parseInt(temp.substring(0, 4));
			int month = Integer.parseInt(temp.substring(5, 7));
			int day = Integer.parseInt(temp.substring(8, 10));

			Calendar validator = Calendar.getInstance();
			validator.set(year, month-1, day);
			validator.setLenient(false);
			validator.getTime();
			
		}catch(NumberFormatException e){
			LBLStartDate.setIcon(BAD);
			valid = false;
		}catch(Exception e){
			LBLStartDate.setIcon(BAD);
			valid = false;
		}
		else LBLStartDate.setIcon(null);
		
		
		
		
		
		//validate 	End date
		temp = textFieldEndDate.getText();
		if(!temp.equals(""))
		try
		{
			if ((temp.charAt(4) == '-' && temp.charAt(7) == '-') || (temp.charAt(4) == '/' && temp.charAt(7) == '/') || (temp.charAt(4) == '.' && temp.charAt(7) == '.')) 
				LBLEndDate.setIcon(OK);
			else {
				LBLEndDate.setIcon(BAD);
				valid = false;
			}
			
			int year = Integer.parseInt(temp.substring(0, 4));
			int month = Integer.parseInt(temp.substring(5, 7));
			int day = Integer.parseInt(temp.substring(8, 10));

			Calendar validator = Calendar.getInstance();
			validator.set(year, month-1, day);
			validator.setLenient(false);
			validator.getTime();
			
		}catch(NumberFormatException e){
			LBLEndDate.setIcon(BAD);
			valid = false;
		}catch(Exception e){
			LBLEndDate.setIcon(BAD);
			valid = false;
		}
		else LBLEndDate.setIcon(null);
		
		
		//TODO VERIFY THAT RETURN DATE IS BEFOR ACTUAL DATE
		
		if (fromOK){
		
			if (textFieldEndDate.equals(""))
			{
				LBLEndDate.setIcon(BAD);
				lblwerning.setVisible(true);
				valid = false;
			}
			if (textFieldStartDate.equals(""))
			{
				LBLStartDate.setIcon(BAD);
				lblwerning.setVisible(true);
				valid = false;
			}
		}
		
		
		if (valid && fromOK){
			ArrayList<String> index = new ArrayList<String>();
			ArrayList<Object> data = new ArrayList<Object>();
		
			
			index.add("LOAN_DATE");
			data.add(textFieldStartDate.getText());

			index.add("ACTUAL_RETURN_DATE");
			data.add(textFieldEndDate.getText());
			
			index.add("ALL");
			data.add("REPORT");
			
			
			Message toSend = new Message(index, data, action);
			
			if (parent.sendMessageToServer(toSend) == 1){//message sent successfully
				parent.SetLoading(true);
								
			}else{//message send failed
				//TODO verify extream situation
			}
			
		
	}


	}
	
}
